package com.lnsf.controller;

/**
 * author：Daen
 * 2017.07.26
 * 
 */
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lnsf.entity.Orders;
import com.lnsf.service.IOrdersService;


@Controller
public class OrdersController {

	@Autowired
	private IOrdersService iOrdersService;
	@ResponseBody
	@RequestMapping("/findOrders")
	public String findOrders(Integer id){
		Orders orders = iOrdersService.getOrders(id);
		System.out.println("findOrders :"+orders);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("/saveOrders")
	public String saveOrders(Orders orders){
		Orders orders1 =iOrdersService.saveOrders(orders);
		System.out.println("saveOrders :"+orders1);
		return "success";
	}
	@RequestMapping(value="/order/{id}",method=RequestMethod.DELETE)
	public String delete(@PathVariable("id") Integer id){
		iOrdersService.delete(id);
		return "redirect:/orders";
	}
	
	@RequestMapping("/orders")
	public String list(@RequestParam(value="pageNo", required=false, defaultValue="1") String pageNoStr, 
			Map<String, Object> map){
		int pageNo = 1;
		
		try {
			//对 pageNo 的校验
			pageNo = Integer.parseInt(pageNoStr);
			if(pageNo < 1){
				pageNo = 1;
			}
		} catch (Exception e) {}
		
		Page<Orders> page = iOrdersService.getPage(pageNo, 5);
		map.put("page", page);
		System.out.println(page);
		return "list";
	}
	
	
	@ResponseBody
	@RequestMapping("/updateOrders")
	public String updateOrders(int id,String describle){
		int result = iOrdersService.updateOrders(id,describle);
		System.out.println("updateOrders success : "+result);
		return "success";
	}
}
